<template>
  <div class="design-editor-container">
    <div class="editor-header">
      <div class="editor-logo">
        <h2>AI绘画平台</h2>
      </div>
      <div class="editor-tabs">
        <div class="tab-items">
          <div class="tab-item active">
            <span>AI设计工具</span>
          </div>
          <div class="tab-item">
            <span>AI绘画</span>
          </div>
          <div class="tab-item">
            <span>AI绘画工具</span>
          </div>
        </div>
      </div>
      <div class="editor-actions">
        <el-button type="text" icon="el-icon-user">登录</el-button>
        <el-button type="text" icon="el-icon-user">注册</el-button>
        <el-switch v-model="darkMode" active-color="#13ce66" inactive-color="#ff4949" />
        <el-button icon="el-icon-setting" circle></el-button>
        <el-button icon="el-icon-refresh" circle></el-button>
      </div>
    </div>
    
    <!-- 主要编辑区域 -->
    <div class="editor-main">
      <!-- 左侧导航菜单 -->
      <div class="editor-nav">
        <div class="nav-items">
          <router-link to="/" class="nav-item">
            <i class="el-icon-s-home"></i>
            <span>首页</span>
          </router-link>
          <router-link to="/commerce" class="nav-item">
            <i class="el-icon-shopping-cart-2"></i>
            <span>电商</span>
          </router-link>
          <router-link to="/tools" class="nav-item active">
            <i class="el-icon-s-tools"></i>
            <span>设计工具</span>
          </router-link>
          <router-link to="/entertainment" class="nav-item">
            <i class="el-icon-film"></i>
            <span>娱乐</span>
          </router-link>
          <router-link to="/life" class="nav-item">
            <i class="el-icon-s-shop"></i>
            <span>生活</span>
          </router-link>
          <router-link to="/my" class="nav-item">
            <i class="el-icon-user"></i>
            <span>我的</span>
          </router-link>
        </div>
      </div>
      
      <!-- 主要设计区域 -->
      <div class="editor-content">
        <!-- 顶部工具栏（模仿截图中的标签） -->
        <div class="design-toolbar">
          <div class="tool-tabs">
            <div class="tool-tab active">
              <span>模版</span>
            </div>
            <div class="tool-tab">
              <span>文字</span>
            </div>
            <div class="tool-tab">
              <span>素材</span>
            </div>
          </div>
          
          <div class="design-actions">
            <el-button-group>
              <el-button size="small" icon="el-icon-copy-document">复制</el-button>
              <el-button size="small" icon="el-icon-delete">删除</el-button>
              <el-button size="small" icon="el-icon-top">移到顶部</el-button>
              <el-button size="small" icon="el-icon-bottom">移到底部</el-button>
            </el-button-group>
            
            <div class="action-buttons">
              <el-button size="small" type="primary">保存</el-button>
              <el-button size="small">导出</el-button>
              <el-button size="small">预览</el-button>
            </div>
          </div>
        </div>
        
        <div class="design-workspace">
          <!-- 左侧内容面板 -->
          <div class="content-panel">
            <h4>基础要素</h4>
            <div class="elements-grid">
              <div class="element-item">
                <div class="element-icon">T</div>
                <div class="element-label">文字</div>
              </div>
              <div class="element-item">
                <div class="element-icon">□</div>
                <div class="element-label">矩形</div>
              </div>
              <div class="element-item">
                <div class="element-icon">○</div>
                <div class="element-label">圆形</div>
              </div>
              <div class="element-item">
                <div class="element-icon">△</div>
                <div class="element-label">三角形</div>
              </div>
              <div class="element-item">
                <div class="element-icon">⬡</div>
                <div class="element-label">多边形</div>
              </div>
            </div>
            
            <h4>绘制元素</h4>
            <div class="elements-grid">
              <div class="element-item">
                <div class="element-icon"><i class="el-icon-brush"></i></div>
                <div class="element-label">画笔</div>
              </div>
              <div class="element-item">
                <div class="element-icon">╱</div>
                <div class="element-label">直线</div>
              </div>
            </div>
            
            <h4>快捷导航</h4>
            <div class="elements-grid">
              <div class="element-item">
                <div class="element-icon"><i class="el-icon-set-up"></i></div>
                <div class="element-label">网格</div>
              </div>
              <div class="element-item">
                <div class="element-icon"><i class="el-icon-ruler"></i></div>
                <div class="element-label">标尺</div>
              </div>
            </div>
            
            <h4>配色</h4>
            <div class="color-picker">
              <el-color-picker v-model="selectedColor" />
            </div>
            
            <h4>素材</h4>
            <div class="materials-grid">
              <div class="material-item" v-for="(material, index) in materials" :key="index">
                <img :src="material.url" :alt="material.name" />
              </div>
            </div>
          </div>
          
          <!-- 中央画布区域 -->
          <div class="canvas-area">
            <canvas ref="fabricCanvas" class="fabric-canvas"></canvas>
          </div>
          
          <!-- 右侧属性面板 -->
          <div class="properties-panel">
            <div class="properties-header">
              <el-button-group>
                <el-button size="small" type="primary">属性</el-button>
                <el-button size="small">AI文生图</el-button>
                <el-button size="small">AI图生图</el-button>
              </el-button-group>
            </div>
            
            <div class="properties-content">
              <div v-if="selectedObject" class="property-sections">
                <div class="property-section">
                  <h4>位置与大小</h4>
                  <div class="property-group">
                    <div class="property-item">
                      <label>X轴</label>
                      <el-input-number v-model="selectedObjectProps.left" size="small" :min="0" />
                    </div>
                    <div class="property-item">
                      <label>Y轴</label>
                      <el-input-number v-model="selectedObjectProps.top" size="small" :min="0" />
                    </div>
                  </div>
                  <div class="property-group">
                    <div class="property-item">
                      <label>宽度</label>
                      <el-input-number v-model="selectedObjectProps.width" size="small" :min="1" />
                    </div>
                    <div class="property-item">
                      <label>高度</label>
                      <el-input-number v-model="selectedObjectProps.height" size="small" :min="1" />
                    </div>
                  </div>
                </div>
                
                <div class="property-section">
                  <h4>旋转</h4>
                  <el-slider v-model="selectedObjectProps.angle" :min="0" :max="360" />
                </div>
                
                <div class="property-section">
                  <h4>外观</h4>
                  <div class="property-group">
                    <div class="property-item">
                      <label>填充</label>
                      <el-color-picker v-model="selectedObjectProps.fill" />
                    </div>
                    <div class="property-item">
                      <label>描边</label>
                      <el-color-picker v-model="selectedObjectProps.stroke" />
                    </div>
                  </div>
                  <div class="property-item">
                    <label>描边宽度</label>
                    <el-input-number v-model="selectedObjectProps.strokeWidth" size="small" :min="0" :max="20" />
                  </div>
                  <div class="property-item">
                    <label>透明度</label>
                    <el-slider v-model="selectedObjectProps.opacity" :min="0" :max="1" :step="0.01" />
                  </div>
                </div>
                
                <div v-if="isTextObject" class="property-section">
                  <h4>文字</h4>
                  <div class="property-item">
                    <label>文本内容</label>
                    <el-input v-model="selectedObjectProps.text" type="textarea" rows="2" />
                  </div>
                  <div class="property-group">
                    <div class="property-item">
                      <label>字体</label>
                      <el-select v-model="selectedObjectProps.fontFamily" size="small">
                        <el-option value="Arial" label="Arial" />
                        <el-option value="Times New Roman" label="Times New Roman" />
                        <el-option value="SimSun" label="宋体" />
                        <el-option value="Microsoft YaHei" label="微软雅黑" />
                      </el-select>
                    </div>
                    <div class="property-item">
                      <label>字号</label>
                      <el-input-number v-model="selectedObjectProps.fontSize" :min="8" :max="120" size="small" />
                    </div>
                  </div>
                  <div class="text-style-buttons">
                    <el-button-group>
                      <el-button 
                        size="small" 
                        :type="selectedObjectProps.fontWeight === 'bold' ? 'primary' : ''" 
                      >
                        B
                      </el-button>
                      <el-button 
                        size="small" 
                        :type="selectedObjectProps.fontStyle === 'italic' ? 'primary' : ''" 
                      >
                        I
                      </el-button>
                      <el-button 
                        size="small" 
                        :type="selectedObjectProps.underline ? 'primary' : ''" 
                      >
                        U
                      </el-button>
                    </el-button-group>
                    
                    <div class="text-align-buttons">
                      <el-button-group>
                        <el-button 
                          size="small" 
                          :type="selectedObjectProps.textAlign === 'left' ? 'primary' : ''" 
                        >
                          <i class="el-icon-align-left"></i>
                        </el-button>
                        <el-button 
                          size="small" 
                          :type="selectedObjectProps.textAlign === 'center' ? 'primary' : ''" 
                        >
                          <i class="el-icon-align-center"></i>
                        </el-button>
                        <el-button 
                          size="small" 
                          :type="selectedObjectProps.textAlign === 'right' ? 'primary' : ''" 
                        >
                          <i class="el-icon-align-right"></i>
                        </el-button>
                      </el-button-group>
                    </div>
                  </div>
                </div>
              </div>
              
              <div v-else class="no-selection">
                <p>请选择一个对象来编辑属性</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed, onUnmounted, nextTick } from 'vue';
import * as fabric from "fabric";

// Canvas reference
const fabricCanvas = ref(null);
let canvas = null;

// UI state
const darkMode = ref(false);
const selectedColor = ref('#FF5722');

// 示例材料
const materials = [
  { name: '花朵', url: '/materials/flower.svg' },
  { name: '星星', url: '/materials/star.svg' },
  { name: '图表', url: '/materials/chart.svg' },
];

// Canvas state
const selectedObject = ref(null);

// 计算属性，判断是否为文本对象
const isTextObject = computed(() => {
  return selectedObject.value && selectedObject.value.type === 'text';
});

// 选中对象的属性
const selectedObjectProps = reactive({
  left: 0,
  top: 0,
  width: 0,
  height: 0,
  angle: 0,
  fill: '#000000',
  stroke: '#000000',
  strokeWidth: 0,
  opacity: 1,
  fontFamily: 'Arial',
  fontSize: 24,
  text: '',
  fontWeight: 'normal',
  fontStyle: 'normal',
  underline: false,
  textAlign: 'left'
});

// 初始化画布
onMounted(async () => {
  await nextTick();
  if (fabricCanvas.value) {
    canvas = new fabric.Canvas(fabricCanvas.value, {
      width: 600,
      height: 600,
      backgroundColor: '#FFFFFF'
    });
    
    // 设置选择事件
    canvas.on('selection:created', handleObjectSelected);
    canvas.on('selection:updated', handleObjectSelected);
    canvas.on('selection:cleared', handleSelectionCleared);
  }
});

onUnmounted(() => {
  if (canvas) {
    canvas.dispose();
  }
});

// 处理选择事件
function handleObjectSelected() {
  selectedObject.value = canvas.getActiveObject();
  updateSelectedObjectProps();
}

function handleSelectionCleared() {
  selectedObject.value = null;
}

function updateSelectedObjectProps() {
  if (!selectedObject.value) return;
  
  // 更新基本属性
  selectedObjectProps.left = Math.round(selectedObject.value.left);
  selectedObjectProps.top = Math.round(selectedObject.value.top);
  selectedObjectProps.width = Math.round(selectedObject.value.width * selectedObject.value.scaleX);
  selectedObjectProps.height = Math.round(selectedObject.value.height * selectedObject.value.scaleY);
  selectedObjectProps.angle = Math.round(selectedObject.value.angle);
  selectedObjectProps.fill = selectedObject.value.fill;
  selectedObjectProps.stroke = selectedObject.value.stroke || '#000000';
  selectedObjectProps.strokeWidth = selectedObject.value.strokeWidth || 0;
  selectedObjectProps.opacity = selectedObject.value.opacity;
  
  // 更新文本特定属性
  if (isTextObject.value) {
    selectedObjectProps.text = selectedObject.value.text;
    selectedObjectProps.fontFamily = selectedObject.value.fontFamily;
    selectedObjectProps.fontSize = selectedObject.value.fontSize;
    selectedObjectProps.fontWeight = selectedObject.value.fontWeight;
    selectedObjectProps.fontStyle = selectedObject.value.fontStyle;
    selectedObjectProps.underline = selectedObject.value.underline;
    selectedObjectProps.textAlign = selectedObject.value.textAlign;
  }
}
</script>

<style scoped>
.design-editor-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 头部样式 */
.editor-header {
  height: 50px;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 15px;
}

.editor-logo {
  width: 150px;
}

.editor-logo h2 {
  margin: 0;
  font-size: 18px;
  color: #409EFF;
}

.editor-tabs {
  flex: 1;
  display: flex;
  justify-content: center;
}

.tab-items {
  display: flex;
}

.tab-item {
  padding: 0 15px;
  line-height: 50px;
  cursor: pointer;
  position: relative;
  color: #606266;
}

.tab-item.active {
  color: #409EFF;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #409EFF;
}

.editor-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* 主要编辑区域 */
.editor-main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* 左侧导航栏 */
.editor-nav {
  width: 60px;
  background-color: #304156;
  display: flex;
  flex-direction: column;
}

.nav-items {
  padding-top: 10px;
}

.nav-item {
  height: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #bfcbd9;
  text-decoration: none;
  cursor: pointer;
}

.nav-item:hover {
  background-color: #263445;
}

.nav-item.active {
  background-color: #1f2d3d;
  color: #409EFF;
}

.nav-item i {
  font-size: 20px;
  margin-bottom: 4px;
}

.nav-item span {
  font-size: 12px;
}

/* 内容区域 */
.editor-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 工具栏 */
.design-toolbar {
  height: 50px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
}

.tool-tabs {
  display: flex;
}

.tool-tab {
  padding: 0 15px;
  height: 36px;
  line-height: 36px;
  background-color: #e8e8e8;
  color: #606266;
  cursor: pointer;
  border-radius: 4px;
  margin-right: 5px;
}

.tool-tab.active {
  background-color: #FF5722;
  color: white;
}

.design-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* 设计工作区 */
.design-workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* 左侧内容面板 */
.content-panel {
  width: 200px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  padding: 15px;
  overflow-y: auto;
}

.content-panel h4 {
  font-size: 14px;
  margin: 15px 0 8px 0;
  color: #606266;
}

.content-panel h4:first-child {
  margin-top: 0;
}

.elements-grid, .materials-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.element-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.element-item:hover {
  background-color: #f5f5f5;
  border-color: #ccc;
}

.element-icon {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  margin-bottom: 4px;
}

.element-label {
  font-size: 12px;
  color: #606266;
}

.material-item {
  height: 50px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
}

.material-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.color-picker {
  margin: 10px 0;
}

/* 画布区域 */
.canvas-area {
  flex: 1;
  background-color: #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

.fabric-canvas {
  background-color: white;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

/* 右侧属性面板 */
.properties-panel {
  width: 240px;
  background-color: #fff;
  border-left: 1px solid #ddd;
  display: flex;
  flex-direction: column;
}

.properties-header {
  padding: 10px;
  border-bottom: 1px solid #eee;
}

.properties-content {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
}

.property-sections {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.property-section {
  background-color: #f9f9f9;
  border-radius: 4px;
  padding: 10px;
}

.property-section h4 {
  font-size: 14px;
  margin: 0 0 10px 0;
  color: #606266;
}

.property-group {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.property-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-bottom: 10px;
}

.property-item label {
  font-size: 12px;
  color: #909399;
  margin-bottom: 5px;
}

.text-style-buttons, .text-align-buttons {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}

.no-selection {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  font-size: 14px;
}
</style>